package com.itheima.reggie.common;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.HashMap;
import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, "reggie") //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        try {
            return Jwts.parser().setSigningKey("reggie")
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        // 制作token
        /*Map claims = new HashMap();
        claims.put("id",10010);
        claims.put("phone","13800138001");
        String token = JwtUtil.createToken(claims);
        System.out.println(token);*/


        // 解析token
        String token ="eyJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjEzODAwMTM4MDAxIiwiaWQiOjEwMDEwfQ.L2q6cl2gX4u1sApmmAGwf3xo7ZDd6Pu-wtuYtodVY1Q";
        Map claims = JwtUtil.parseToken(token);
        System.out.println(claims);

    }
}